//
// Created by dxyinme on 2021/10/4.
//
#include "gtest/gtest.h"
#include "v_sort_list.h"
#include "util.h"

#include "v_sort_test_comm.h"

class VsortTest : public VsortTestComm {
  void SetUp() override {
    VsortTestComm::SetUp();
  }

  void TearDown() override {
    VsortTestComm::TearDown();
  }
};

TEST_F(VsortTest, testVsort120w) {
  int len = 1200000;
  auto* o = Vsort::NewRandomIntVList(len);
  auto* res = Vsort::SortVListExThread(o, 300000, VsortTestComm::GetThreadPoolInstance());
  auto* test_ordered = res;
  while (test_ordered->nxt != nullptr) {
    EXPECT_LE(test_ordered->val, test_ordered->nxt->val);
    test_ordered = test_ordered->nxt;
  }
}

TEST_F(VsortTest, testVsort120w6Thread) {
  int len = 1200000;
  auto* o = Vsort::NewRandomIntVList(len);
  auto* res = Vsort::SortVListExThread(o, 200000, VsortTestComm::GetThreadPoolInstance());
  auto* test_ordered = res;
  while (test_ordered->nxt != nullptr) {
    EXPECT_LE(test_ordered->val, test_ordered->nxt->val);
    test_ordered = test_ordered->nxt;
  }
}

TEST_F(VsortTest, testVsort120w2Thread) {
  int len = 1200000;
  auto* o = Vsort::NewRandomIntVList(len);
  auto* res = Vsort::SortVListExThread(o, 600000, VsortTestComm::GetThreadPoolInstance());
  auto* test_ordered = res;
  while (test_ordered->nxt != nullptr) {
    EXPECT_LE(test_ordered->val, test_ordered->nxt->val);
    test_ordered = test_ordered->nxt;
  }
}

TEST_F(VsortTest, testVsort120w1Thread) {
  int len = 1200000;
  auto* o = Vsort::NewRandomIntVList(len);
  auto* res = Vsort::SortVListExThread(o, 1200000, VsortTestComm::GetThreadPoolInstance());
  auto* test_ordered = res;
  while (test_ordered->nxt != nullptr) {
    EXPECT_LE(test_ordered->val, test_ordered->nxt->val);
    test_ordered = test_ordered->nxt;
  }
}

TEST_F(VsortTest, testVsort1200w1Thread) {
  int len = 12000000;
  auto* o = Vsort::NewRandomIntVList(len);
  auto* res = Vsort::SortVListExThread(o, 12000000, VsortTestComm::GetThreadPoolInstance());
  auto* test_ordered = res;
  while (test_ordered->nxt != nullptr) {
    EXPECT_LE(test_ordered->val, test_ordered->nxt->val);
    test_ordered = test_ordered->nxt;
  }
}

TEST_F(VsortTest, testVsort1200w) {
  int len = 12000000;
  auto* o = Vsort::NewRandomIntVList(len);
  auto* res = Vsort::SortVListExThread(o, 3000000, VsortTestComm::GetThreadPoolInstance());
  auto* test_ordered = res;
  while (test_ordered->nxt != nullptr) {
    EXPECT_LE(test_ordered->val, test_ordered->nxt->val);
    test_ordered = test_ordered->nxt;
  }
}

TEST_F(VsortTest, testVectorSort120w) {
  int len = 1200000;
  std::vector<int> oc;
  oc.reserve(len);
  for (int i = 0; i < len; i++) {
    oc.push_back(i);
  }
  std::shuffle(oc.begin(), oc.end(), std::mt19937(std::random_device()()));
  sort(oc.begin(), oc.end());
  for (int i = 0; i + 1 < oc.size(); i++) {
    EXPECT_LE(oc[i], oc[i+1]);
  }
}

TEST_F(VsortTest, testVectorSort1200w) {
  int len = 12000000;
  std::vector<int> oc;
  oc.reserve(len);
  for (int i = 0; i < len; i++) {
    oc.push_back(i);
  }
  std::shuffle(oc.begin(), oc.end(), std::mt19937(std::random_device()()));
  sort(oc.begin(), oc.end());
  for (int i = 0; i + 1 < oc.size(); i++) {
    EXPECT_LE(oc[i], oc[i+1]);
  }
}